<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\Log;
use think\Request;

class IpQuery extends Controller
{

    public function __construct()
    {
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
        // parent::__construct();
        // set_cors();
    }
    //客户端进入游戏前ip效验
    public function info(){
    
        $ip = Request()->ip();
        

        // $ip = '161.64.5.128';
        if (empty($ip)){
            exit(json_encode(['code'=>0,'msg'=>'获取ip失败','data'=>[]]));
        }
        
        $user_ip_info = db('user_ip_info','db_operatelog')->where(['ip'=>$ip])->find();
        if($user_ip_info){
            exit(json_encode(['code'=>1,'msg'=>'','data'=>$user_ip_info],JSON_UNESCAPED_UNICODE));
        }

        $host = "http://qryip.market.alicloudapi.com";
        $path = "/lundear/qryip";
        $method = "GET";
        $appcode = "a6625c18746c4eba8e8dbef479703b18";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "ip=".$ip;
        $url = $host . $path . "?" . $querys;

            // echo $url.PHP_EOL;
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            // curl_setopt($curl, CURLOPT_HEADER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            $obj = curl_exec($curl);
            if(empty($obj)){
                exit(json_encode(['code'=>0,'msg'=>'curl_exec false','data'=>[]]));
            }
            $data = json_decode($obj,true);
            if(empty($data) || $data['status'] != 0){
                exit(json_encode(['code'=>0,'msg'=>'status false','data'=>[]]));
            }
            $data = [
                'add_time'=>date('Y-m-d H:i:s'),
                'lat'=>$data['result']['location']['lat'],
                'lng'=>$data['result']['location']['lng'],

                'nation'=>$data['result']['ad_info']['nation'],
                'province'=>$data['result']['ad_info']['province'],
                'city'=>$data['result']['ad_info']['city'],
                'district'=>$data['result']['ad_info']['district'],
                'ip'=>$ip,
            ];
            db('user_ip_info','db_operatelog')->insert($data);

            exit(json_encode(['code'=>1,'msg'=>'','data'=>$data,JSON_UNESCAPED_UNICODE]));
    }
    public function query(Request $request){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
        $ip = Request()->ip();
        $param = $request->param();
        if (empty($param['user_id']) || empty($ip)){
            exit(json_encode(['code'=>0,'msg'=>'缺少参数','data'=>[]]));
        }

        $accountsInfoModel = Db::connect('db_config2')->table('accounts_info')->where(['UserID'=>$param['user_id']])->find();
        if(!$accountsInfoModel){
            exit(json_encode(['code'=>0,'msg'=>'未找到该用户','data'=>[]]));
        }
        //查询用户是否绑定ip
        $user_ip_info = db('user_ip_info','db_operatelog')->where(['user_id'=>$param['user_id']])->find();
        if($user_ip_info){
            exit(json_encode(['code'=>1,'msg'=>'','data'=>$user_ip_info],JSON_UNESCAPED_UNICODE));
        }
        //用ip查询 更新用户
        $user_ip_info = db('user_ip_info','db_operatelog')->where(['ip'=>$ip])->find();
        if($user_ip_info){
            db('user_ip_info','db_operatelog')->where(['ip'=>$ip])->update(['user_id'=>$param['user_id']]);
            exit(json_encode(['code'=>1,'msg'=>'','data'=>$user_ip_info],JSON_UNESCAPED_UNICODE));
        }

        //以上都查不到从新调接口查询
        $host = "http://qryip.market.alicloudapi.com";
        $path = "/lundear/qryip";
        $method = "GET";
        $appcode = "a6625c18746c4eba8e8dbef479703b18";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "ip=".$ip;
        $url = $host . $path . "?" . $querys;
            // echo $url.PHP_EOL;
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            // curl_setopt($curl, CURLOPT_HEADER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            $obj = curl_exec($curl);
            if(empty($obj)){
                exit(json_encode(['code'=>0,'msg'=>'curl_exec false','data'=>[]]));
            }
            $data = json_decode($obj,true);
            if(empty($data) || $data['status'] != 0){
                exit(json_encode(['code'=>0,'msg'=>'status false','data'=>[]]));
            }
            $data = [
                'add_time'=>date('Y-m-d H:i:s'),
                'user_id'=>$param['user_id'],
                'lat'=>$data['result']['location']['lat'],
                'lng'=>$data['result']['location']['lng'],

                'nation'=>$data['result']['ad_info']['nation'],
                'province'=>$data['result']['ad_info']['province'],
                'city'=>$data['result']['ad_info']['city'],
                'district'=>$data['result']['ad_info']['district'],
                'ip'=>$ip,
            ];
            db('user_ip_info','db_operatelog')->insert($data);
            exit(json_encode(['code'=>1,'msg'=>'','data'=>$data,JSON_UNESCAPED_UNICODE]));
    }


    
    public function query_old(Request $request){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

        $param = $request->param();
        if (empty($param['user_id'])){
            exit(json_encode(['code'=>0,'msg'=>'缺少参数','data'=>[]]));
        }

        $accountsInfoModel = Db::connect('db_config2')->table('accounts_info')->where(['UserID'=>$param['user_id']])->find();
        if(!$accountsInfoModel){
            exit(json_encode(['code'=>0,'msg'=>'未找到该用户','data'=>[]]));
        }
        $user_ip_info = db('user_ip_info','db_operatelog')->where(['user_id'=>$accountsInfoModel['UserID']])->find();
        if($user_ip_info){
            exit(json_encode(['code'=>1,'msg'=>'','data'=>$user_ip_info],JSON_UNESCAPED_UNICODE));
        }

        $host = "http://qryip.market.alicloudapi.com";
        $path = "/lundear/qryip";
        $method = "GET";
        $appcode = "a6625c18746c4eba8e8dbef479703b18";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        
        if(empty($accountsInfoModel['RegisterIP'])){
            exit(json_encode(['code'=>0,'msg'=>'没有注册ip','data'=>[]],JSON_UNESCAPED_UNICODE));
            
        }
        $querys = "ip=".trim($accountsInfoModel['RegisterIP']);
        $url = $host . $path . "?" . $querys;

            // echo $url.PHP_EOL;
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            // curl_setopt($curl, CURLOPT_HEADER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            $obj = curl_exec($curl);
            if(empty($obj)){
                exit(json_encode(['code'=>0,'msg'=>'curl_exec false','data'=>[]]));
            }
            $data = json_decode($obj,true);
            if(empty($data) || $data['status'] != 0){
                exit(json_encode(['code'=>0,'msg'=>'status false','data'=>[]]));
            }
            $data = [
                'add_time'=>date('Y-m-d H:i:s'),
                'user_id'=>$accountsInfoModel['UserID'],
                'lat'=>$data['result']['location']['lat'],
                'lng'=>$data['result']['location']['lng'],

                'nation'=>$data['result']['ad_info']['nation'],
                'province'=>$data['result']['ad_info']['province'],
                'city'=>$data['result']['ad_info']['city'],
                'district'=>$data['result']['ad_info']['district'],
            ];
            db('user_ip_info','db_operatelog')->insert($data);

            exit(json_encode(['code'=>1,'msg'=>'','data'=>$data,JSON_UNESCAPED_UNICODE]));

    }
}

